package cn.com.greatwall.flowable.rest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.flowable.service.ProcessDefinitionService;
import cn.com.greatwall.flowable.service.dto.IdentityLinkDto;
import cn.com.greatwall.logging.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2020-11-19 16:25:11
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Flowable：流程授权")
@RequestMapping("/api/flowable/definitionIdentityLink")
public class ProcessDefinitionIdentityLinkController {
    private final ProcessDefinitionService processDefinitionService;

    @Log("查询流程授权")
    @ApiOperation("查询流程授权")
    @GetMapping
    @PreAuthorize("@el.check('definitionIdentityLink:list')")
    public ResponseEntity<Object> query(@RequestParam String processDefinitionId) {
        return new ResponseEntity<>(processDefinitionService.queryProcessDefinitionIdentityLink(processDefinitionId), HttpStatus.OK);
    }

    @Log("新增流程授权")
    @ApiOperation("新增流程授权")
    @PostMapping
    @PreAuthorize("@el.check('definitionIdentityLink:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody IdentityLinkDto resources) {
        processDefinitionService.saveProcessDefinitionIdentityLink(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("删除流程授权")
    @ApiOperation("删除流程授权")
    @DeleteMapping
    @PreAuthorize("@el.check('definitionIdentityLink:del')")
    public ResponseEntity<Object> delete(@RequestParam String processDefinitionId, @RequestParam String identityId,
            @RequestParam String identityType) {
        processDefinitionService.deleteProcessDefinitionIdentityLink(processDefinitionId, identityId, identityType);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
